"  vim: foldmethod=marker  

call pathogen#runtime_append_all_bundles() 

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" General                                                 {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Enable filetype plugin                                    {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
filetype on
filetype plugin on
filetype indent on
syntax enable "Enable syntax hl
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Fileformats && encoding                                   {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Favorite filetypes
if has("win32")
  set ffs=dos,unix,mac
else
  set ffs=unix,dos,mac
endif
set fileencodings=utf-8,gbk,ucs-bom,cp936

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" VIM visual setting                                       {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set guioptions=
set background=light
colorscheme moria
set novisualbell
set noerrorbells 
set t_vb=
autocmd VimEnter * set vb t_vb=
set showmatch 
if has("win32")
  set guifont=YaHei\ Consolas\ Hybrid:h12
else 
  set guifont=YaHei\ Consolas\ Hybrid\ 12
endif 
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" VIM general setting                                       {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set pastetoggle=<F3>
set scrolloff=2 "Set 2 lines to the curors - when moving vertical..
set wildmenu "Turn on WiLd menu
set ruler "Always show current position
set number
set colorcolumn=+1
"set lazyredraw "Do not redraw, when running macros.. lazyredraw
set hid "Change buffer - without saving
set backspace=eol,start,indent "Set backspace
"set whichwrap+=<,>,h,l "Bbackspace and cursor keys wrap to
set incsearch
set magic "Set magic on
set mat=2 "How many tenths of a second to blink
set hlsearch "Highlight search things
set isk+=_,$,@,%,#,- " none of these should be word dividers, so make them not be
set autoread "Set to auto read when a file is changed from the outside
set mouse=a "Have the mouse enabled all the time:
set history=400 "Sets how many lines of history VIM har to remember
if has("win32")
  set linespace=0
endif
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Statusline                                                {{{
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set laststatus=2 "Always hide the statusline
function! CurDir()
  let curdir = substitute(getcwd(), '/Users/amir/', "~/", "g")
  return curdir
endfunction
set statusline=\ %F%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ \ Line:\ %l/%L:%c "Format the statusline

try
  set switchbuf=usetab
  set stal=2
catch
endtry
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Files and backups and undo                                {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set undofile
"Turn backup off
set nobackup
set nowb
set noswapfile
if has("win32")
  set undodir=D:\\Program\ Files\\Vim\\vimfiles\\undo
else
  set undodir=/home/wsn/.vim/undo 
endif
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Folding                                                  {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Enable folding, I find it very useful
"set nofen
"set fdl=0
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Text options (tab,textwidth,linebreak)                   {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set expandtab
set shiftwidth=2

au FileType html,xml,vim,javascript setl shiftwidth=2
au FileType html,xml,vim,javascript setl tabstop=2
au FileType java setl shiftwidth=4
au FileType java setl tabstop=4
au FileType python set shiftwidth=4
au FileType python set tabstop=4

"xml folding
let g:xml_syntax_folding=1
au FileType xml setlocal foldmethod=syntax

set smarttab
set linebreak
set textwidth=500
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Indent                                                   {{{
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set autoindent "Auto indent
set smartindent "Smart indet
set cindent "C-style indeting
set wrap "Wrap lines
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Script and platform dependent setting                     {{{
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

if has("autocmd") && exists("+omnifunc")
	autocmd Filetype *
		    \	if &omnifunc == "" |
		    \		setlocal omnifunc=syntaxcomplete#Complete |
		    \	endif
endif


let xml_use_html = 1
let html_use_css = 1
let html_number_lines = 0
let use_xhtml = 1
let html_no_rendering = 1
let g:netrw_altv = 1

let g:mapleader = ","
let g:NERDTreeHijackNetrw=0
let g:vimwiki_home = "~/mywiki/"
let g:fencview_autodetect = 0
let g:showmarks_include="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
let g:SuperTabRetainCompletionDuration =  "session"
let g:EclimTaglistEnabled = 0
let loaded_matchparen = 1

if has("win32")
  let g:sztool_home="E:\\work\\vim-sztool\\sztools"
  set runtimepath+=E:\\work\\vim-sztool
  let g:vimwiki_list = [ 
      \ {'path': 'F:\My Dropbox\wiki\public',  'path_html': 'F:\My Dropbox\Public\vimwiki' } ,
      \ {'path': 'F:\My Dropbox\wiki\private'} ,
      \ {'path': 'F:\My Dropbox\wiki\blog'} ]
  map <F11> <Esc>:call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>
else
  let g:sztool_home="/project/vim-sztool/sztools"
  set runtimepath+=/project/vim-sztool
  let g:vimwiki_list = [  {'path': '/syncdata/Dropbox/wiki/public',
      \ 'path_html': '/syncdata/Dropbox/Public/vimwiki' } ,
      \ {'path': '/syncdata/Dropbox/wiki/private'} ,
      \ {'path': '/syncdata/Dropbox/wiki/blog'} ]
endif


let g:vimwiki_use_mouse = 1
let g:vimwiki_camel_case = 0
let g:vimwiki_CJK_length = 1

"let g:EasyMotion_mapping_f = ',ef'
let g:EasyMotion_mapping_F = ',a'
let g:EasyMotion_mapping_w = ',e'
let g:EasyMotion_mapping_e = '_e'
let g:EasyMotion_mapping_b = ',x'
let g:EasyMotion_keys = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
let g:EasyMotion_do_shade = 0 

let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1
let Tlist_Use_Right_Window=1

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Less                                                     {{{
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fun! s:NextPage()
  if line(".") != line("$")
    exe "normal! \<C-F>"
  endif
endfun

fun! Less()
  " Used after each command: put cursor at end and display position
  if &wrap
    noremap <SID>L L0:redraw<CR>:file<CR>
    au VimEnter * normal! L0
  else
    noremap <SID>L Lg0:redraw<CR>:file<CR>
    au VimEnter * normal! Lg0
  endif

  noremap <script> <Space> :call <SID>NextPage()<CR><SID>L
  map f <Space>
  map z <Space>
  noremap <script> d <C-D><SID>L
  noremap <script> e <C-E><SID>L
  noremap <script> j <C-E><SID>L
  noremap <script> b <C-B><SID>L
  noremap <script> u <C-U><SID>L
  noremap <script> k <C-Y><SID>L
  map <silent>v :silent call <SID>End()<CR>
  map <silent>q :silent call <SID>End()<CR>
endfun



fun! s:End()
  unmap <Space>
  unmap f
  unmap z
  unmap d
  unmap e
  unmap j
  unmap b
  unmap u
  unmap k
  unmap v
  unmap q
endfun
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vim general mapping                                      {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nmap <leader>q   :q<cr>
nmap <leader>s   :w<cr>
"nmap <leader>bd  :Bclose<cr>
nmap <leader>,   :pyf %<cr>
nmap <leader>cd  :lcd %:p:h<cr>  
nmap <leader>h   :set filetype=html<cr>
nmap <c-x><c-n> :call SuperTabSetCompletionType(g:SuperTabDefaultCompletionType)<cr>

command! -nargs=0 Less     :call Less()
nmap <silent><leader>zl  :call Less()<CR>


if has("win32")
  nmap <leader>v :e $HOME/_vimrc
  autocmd! bufwritepost _vimrc source $HOME/_vimrc 
else
  nmap <leader>v :e $HOME/.vimrc
  autocmd! bufwritepost .vimrc source $HOME/.vimrc 
endif

autocmd BufEnter * :syntax sync fromstart


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Moving around and tabs
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

map <M-h>  gT
map <M-l>  gt

map <leader>tn :tabnew<cr>
map <leader>te :tabedit
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove

"Remap VIM 0
map 0 ^

"Move a line of text using control
nmap <M-j> mz:m+<cr>`z
nmap <M-k> mz:m-2<cr>`z

map <leader>t2 :set shiftwidth=2<cr>
map <leader>t4 :set shiftwidth=4<cr>

"Orginal for all
map <C-n> :cn<cr>
map <C-p> :cp<cr>
map <leader>c :botright cw 10<cr>
map <c-u> <c-l><c-j>:q<cr>:botright cw 10<cr>

"Remove the Windows ^M
"noremap <Leader>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm

"Remove indenting on empty lines
map <F2> :%s/\s*$//g<cr>:noh<cr>''


"command! Bclose call <SID>BufcloseCloseIt()
"function! <SID>BufcloseCloseIt()
"   let l:currentBufNum = bufnr("%")
"   let l:alternateBufNum = bufnr("#")
"
"   if buflisted(l:alternateBufNum)
"     buffer #
"   else
"     bnext
"   endif
"
"   if bufnr("%") == l:currentBufNum
"     new
"   endif
"
"   if buflisted(l:currentBufNum)
"     execute("bwipeout! ".l:currentBufNum)
"   endif
"endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" testing                                                   {{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! RestoreRegister()
  let @" = s:restore_reg
  return ''
endfunction

function! s:Repl()
    let s:restore_reg = @"
    return "p@=RestoreRegister()\<cr>"
endfunction

vnoremap <silent> <expr> p <sid>Repl()

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}


nnoremap <silent> <F9>  :ProjectTree<cr>
nnoremap <silent> <F10> :TagbarToggle<cr>
nnoremap <silent> <F12> :ProjectTreeFind<cr>

au BufNewFile,BufRead *.ftl set ft=html
let g:bufExplorerShowRelativePath=1  " Show relative paths.
let g:bufExplorerSortBy='fullpath'   " Sort by full file path name.
